<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('入参来源设置')" />
    <link th:href="@{/css/common.css}" rel="stylesheet"/>
    <link th:href="@{/css/edit.css}" rel="stylesheet"/>
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
    <style type="text/css">
        .dropdown-submenu {
            position: relative;
        }
        .dropdown-submenu > .dropdown-menu {
            top: 0;
            left: 100%;
            margin-top: -6px;
            margin-left: -1px;
            -webkit-border-radius: 0 6px 6px 6px;
            -moz-border-radius: 0 6px 6px;
            border-radius: 0 6px 6px 6px;
        }
        .dropdown-submenu:hover > .dropdown-menu {
            display: block;
        }
        .dropdown-submenu > a:after {
            display: block;
            content: " ";
            float: right;
            width: 0;
            height: 0;
            border-color: transparent;
            border-style: solid;
            border-width: 5px 0 5px 5px;
            border-left-color: #ccc;
            margin-top: 5px;
            margin-right: -10px;
        }
        .dropdown-submenu:hover > a:after {
            border-left-color: #fff;
        }
        .dropdown-submenu.pull-left {
            float: none;
        }
        .dropdown-submenu.pull-left > .dropdown-menu {
            left: -100%;
            margin-left: 10px;
            -webkit-border-radius: 6px 0 6px 6px;
            -moz-border-radius: 6px 0 6px 6px;
            border-radius: 6px 0 6px 6px;
        }
    </style>
</head>
<body class="gray-bg source-edit">
<input type = "hidden" id="id" value="">
<input type = "hidden" id="pageId" th:value="${pageId}">
<input type = "hidden" id="interfaceFieldId" th:value="${interfaceFieldId}">
<div class="wrapper wrapper-content ">
    <div class="col-md-12" style="padding-left: 0px">
        <div class="title">
            <span></span>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox-content">
                <form class="form-horizontal" id="fieldForm">
                    <input type = "hidden" name="pageId" th:value="${pageId}">
                    <input type = "hidden" name="pageImgId" th:value="${pageImgId}">
                    <input type = "hidden" name="interfaceId" th:value="${interfaceId}">
                    <input type = "hidden" name="objectId" th:value="${interfaceFieldId}">
                    <input type = "hidden" name="objectType" th:value="${type}">
                </form>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script id="formDatailTpl" type="text/template">
    <div class="form-group col-sm-2">
        <div class="col-sm-12" style="margin-left: -16px;">
            <select  name="sourceType" class="form-control select-one">
                <option value="0">--选择--</option>
                <option value="1" {{# if(1==d.sourceType){ }}selected{{# }}}>接口</option>
                <option value="2" {{# if(2==d.sourceType){ }}selected{{# }}}>约定值</option>
                <option value="3" {{# if(3==d.sourceType){ }}selected{{# }}}>缓存</option>
            </select>
        </div>
    </div>
    <div class="form-group col-sm-3" id="select-two">
    </div>
    <div class="form-group col-sm-3" id="select-three">
    </div>
    <div class="form-group col-sm-2" id="select-four">
    </div>
    <div class="form-group col-sm-12">
        <div class="col-sm-12">
            <textarea  name="remark" class="form-control" placeholder="填写备注" style="height: 80px">{{d.remark||''}}</textarea>
        </div>
    </div>
</script>
<script id="interfaceOptionTpl" type="text/template">
    <div class="col-sm-12">
        <input type="hidden" name="sourceInterfaceUrl" value="{{d.sourceInterfaceUrl}}">
        <input type="hidden" name="sourceInterfaceName" value="{{d.sourceInterfaceName}}">
        <select  name="sourceInterfaceId" class="form-control select-{{d.type}} select2-multiple" id="interfaceList">
        <option value="0">--选择接口--</option>
        {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.id}}"
                    data-url="{{a.interfaceUrl}}"
                    url="{{a.interfaceUrl}}"
                    name="{{a.interfaceName}}"
                    data-name="{{a.interfaceName}}" {{# if(d.sourceInterfaceId==a.id){ }}selected{{# }}}
            >{{a.interfaceName}}</option>
        {{# }); }}
        </select>
    </div>
</script>
<script id="conventinalOptionTpl" type="text/template">
    <div class="col-sm-12" id="conventinal">
        <select  name="contractValueType" class="form-control select-two">
        <option value="">--选择类型--</option>
        {{# layui.each(d.data, function(index, a){ }}
        <option value="{{a.index}}" {{# if(d.contractValueType==a.index){ }}selected{{# }}} data-action="{{a.action}}">{{a.label}}</option>
        {{# }); }}
        </select>
    </div>
</script>
<script id="fieldDictOptionTpl" type="text/template">
    <div class="col-sm-12">
        <select  name="dictValue" class="form-control select-three">
            <option value="0">--选择编码CODE--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.dictValue}}" data-enname="{{a.fieldEnName}}" {{# if(d.dictValue==a.dictValue){ }}selected{{# }}}>{{a.dictLabel}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="tableListTpl" type="text/template">
    <input type="hidden" name="tableOrFieldName" value="{{d.tableOrFieldName}}">
    <div class="col-sm-12">
        <select  name="tableOrFieldId" id="tableList" class="form-control select-three select2-multiple">
            <option value="0">--选择表名--</option>
            {{# layui.each(d.data, function(index, a){ }}
                <option value="{{a.id}}" title="{{a.tableEnName}}" {{# if(d.tableOrFieldId==a.id){ }}selected{{# }}}>{{a.tableEnName}}/{{a.tableName}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="pkFieldListTpl" type="text/template">
    <input type="hidden" name="tableOrFieldName" value="{{d.tableOrFieldName}}">
    <div class="col-sm-12">
        <select  name="tableOrFieldId" id="pkFieldList" class="form-control select-three select2-multiple">
            <option value="0">--选择主键名--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.id}}" title="{{a.fieldEnName}}" {{# if(d.tableOrFieldId==a.id){ }}selected{{# }}}>{{a.fieldEnName}}/{{a.fieldName}}（{{a.tableName}}）</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="ObjAttrTpl" type="text/template">
    <div class="col-sm-12">
         <input class="form-control" name="contractValue" placeholder="输入固定入参值" value="{{d.contractValue||''}}">
    </div>
</script>
<script id="queryDictOptionTpl" type="text/template">
    <div class="col-sm-12">
        <select  name="{{d.name||'storageType'}}" class="form-control select-{{d.name||'two'}}">
            <option value="0">--请选择--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.dictValue}}" data-enname="{{a.fieldEnName}}" {{# if(d.storageType==a.dictValue){ }}selected{{# }}}>{{a.dictLabel}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="queryDictTypeOptionTpl" type="text/template">
    <div class="col-sm-12">
        <select  name="dictType" id="dictTypes" class="form-control select-three select2-multiple">
            <option value="0">--选择字典类型--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.dictType}}" data-enname="{{a.fieldEnName}}" {{# if(d.dictType==a.dictType){ }}selected{{# }}}>{{a.dictName}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="queryInterfaceAliasNamesTpl" type="text/template">
    <div class="col-sm-6" id="interfaceAliasNames">
        <select  name="sourcePageImgId" class="form-control">
            <option value="0">--选择编码列表接口关系--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.id}}" {{# if(d.sourcePageImgId==a.id){ }}selected{{# }}}>{{a.aliasName}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="dictCategoryTpl" type="text/template">
    <div class="col-sm-6" id="dictCategory">
        <select  name="dictCategory" class="form-control">
        <option value="0">--选择字典分类--</option>
        {{# layui.each(d.data, function(index, a){ }}
        <option value="{{a.dictValue}}" data-enname="{{a.fieldEnName}}" {{# if(d.dictCategory==a.dictValue){ }}selected{{# }}}>{{a.dictLabel}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="DictListByCategoryTpl" type="text/template">
    <div class="col-sm-12">
        <select  name="dictType" class="form-control select-three" >
            <option value="0">--请选择--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.dictId}}" {{# if(d.tableOrFieldName==a.dictId){ }}selected{{# }}}>{{a.dictName}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="sourceDictTypeTpl" type="text/template">
    <div class="form-group col-sm-5" id="sourceDictType">
        <label class="col-sm-4 control-label" style="text-align: initial;">此处出参默认值：</label>
        <div class="col-sm-8">
            <select  name="sourceDictType" class="form-control" >
                {{# layui.each(d.data, function(index, a){ }}
                <option value="{{a.dictType}}" {{# if(d.sourceDictType==a.dictType){ }}selected{{# }}}>{{a.dictName}}</option>
                {{# }); }}
            </select>
        </div>
    </div>
</script>
<script id="sourceDictValueTpl" type="text/template">
    <div class="form-group col-sm-3" id="sourceDictValue">
        <select  name="sourceDictValue" class="form-control">
            <option value="0">--请选择--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.dictValue}}"  {{# if(d.sourceDictValue==a.dictValue){ }}selected{{# }}}>{{a.dictLabel}}</option>
            {{# }); }}
        </select>
    </div>
</script>
<script id="fieldTpl"  type="text/template">
    <div class="col-sm-12">
        <input type="hidden" name="sourceObjectId" value="{{d.sourceObjectId||''}}" />
        <input type="hidden" name="sourceObjectName" value="{{d.sourceObjectName||''}}" />
        <input type="hidden" name="sourceObjectEnName" value="{{d.sourceObjectEnName||''}}" />
        <input type="hidden" name="sourceObjectFieldId" value="{{d.sourceObjectFieldId||''}}" />
        <input type="hidden" name="sourceObjectFieldName" value="{{d.sourceObjectFieldName||''}}" />
        <input type="hidden" name="sourceObjectFieldEnName" value="{{d.sourceObjectFieldEnName||''}}" />
        <div class="dropdown" >
            <a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-white" data-target="#" href="javascript:;" rel="external nofollow">
                <span id="select-title">选择字段</span> <span class="caret"></span>
            </a>
            <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
            {{# layui.each(d.data, function(index, a){ }}
                {{# if(a.objectType == '2'){ }}
                <li class="dropdown-submenu">
                    <a  class="noclick" href="javascript:;" rel="external nofollow" data-index="{{index}}">{{a.fieldEnName}}/{{a.fieldName}}</a>
                    <ul class="dropdown-menu">
                        {{# if(a.children){ }}
                            {{# layui.each(a.children, function(indexA, b){ }}
                            <li><a data-objid="{{a.id}}"
                                   data-objenname="{{a.fieldEnName}}"
                                   data-objname="{{a.fieldName}}"
                                   data-objfieldid="{{b.id}}"
                                   data-objfieldname="{{b.fieldName}}"
                                   data-objfieldenname="{{b.fieldEnName}}"
                                   href="javascript:;" rel="external nofollow">{{b.fieldEnName}}/{{b.fieldName}}</a></li>
                            {{# }); }}
                        {{# } }}
                    </ul>
                </li>
                {{# } else {}}
                    {{# if(a.id){ }}
                    <li><a href="javascript:;" rel="external nofollow"
                           data-objid="{{a.id}}"
                           data-objenname="{{a.fieldEnName}}"
                           data-objname="{{a.fieldName}}">
                            {{a.fieldEnName}}/{{a.fieldName}}</a></li>
                    {{# } }}
                {{# } }}
            {{# }); }}
            </ul>
        </div>
    </div>
</script>
<script th:inline="javascript">
    var selectOneActions = ['','interfaceList','conventinalOption','queryDict']
    var conventinalData = [
        {index:1,label:"字段枚举类型",action:"queryFieldDict"},
        {index:6,label:"主键标识",action:"queryDictType"},
        {index:2,label:"表名",action:"tableList"},
        {index:3,label:"主键字段名",action:"pkFieldList"},
        {index:4,label:"修订字段名",action:"ObjAttrCode"},
        {index:0,label:"枚举父类",action:"getDictCategory"},
        // {index:5,label:"自定义值",action:"ObjAttr"},
    ];
    parent.ckeckData && $(".title").html(`<span>"${parent.ckeckData.enname}":"${decodeURIComponent(parent.ckeckData.mockdata)}"</span><span>//${parent.ckeckData.name}</span>`)
    $(function () {
        detail();
        $(".reset").click(function () {
            window.location.reload();
        })
        $("body").on("change",".select-one",function () {
            $("#select-three,#select-four").html('');
            $("#sourceDictType,#sourceDictValue").remove();
            $("#select-two").addClass("col-sm-3").removeClass("col-sm-5");
            window[selectOneActions[$(this).val()]]();
        }).on("change",".select-two",function () {
            //接口
            if ($(".select-one").val() == 1) {
                $("input[name='sourceInterfaceUrl']").val($(".select-two option:selected").data("url"));
                $("input[name='sourceInterfaceName']").val($(".select-two option:selected").data("name"));
                $("#sourceDictType,#sourceDictValue").remove();
                queryInterfaceAliasNames();
                queryBffInterfaceField($(this).val());
            } else if ($(".select-one").val() == 2) {
                $("#select-two").addClass("col-sm-3").removeClass("col-sm-5");
                $("#conventinal").addClass("col-sm-12").removeClass("col-sm-6");
                $("#select-three,#select-four").html('');
                $("#dictCategory").remove();
                window[$(".select-two option:selected").data("action")]();
            } else if ($(".select-one").val() == 3) {
                $("#select-four").html('');
                interfaceList("", $(this).val());
            }
        }).on("change","select[name='dictCategory']",function(){
            // 根据字典类型获取字典值
            queryDictType()
        }).on("change",".select-three",function(){
            if ($(".select-one").val()==2 && $(".select-two").val() == 6) {
                 queryDict()
            }
        }).on("change",".select-storage-three",function () {
            $("input[name='sourceInterfaceUrl']").val($(".select-storage-three option:selected").data("url"));
            $("input[name='sourceInterfaceName']").val($(".select-storage-three option:selected").data("name"))
            queryBffInterfaceField($(this).val());
        }).on("click",".dropdown li a:not([class='noclick'])",function () {
            $("#select-title").text($(this).html());
            var data = $(this).data()
            $("input[name='sourceObjectId']").val(data.objid)
            $("input[name='sourceObjectName']").val(data.objname)
            $("input[name='sourceObjectEnName']").val(data.objenname)
            $("input[name='sourceObjectFieldId']").val(data.objfieldid||'')
            $("input[name='sourceObjectFieldName']").val(data.objfieldname||'')
            $("input[name='sourceObjectFieldEnName']").val(data.objfieldenname||'')
        }).on("change","select[name='sourceDictType']",function () {
            queryDict();
        }).on("change","select[name='sourcePageImgId']",function(){
            querySourceInterfaceContainCategory($(this).val())
        })
    })
    function resetHandler() {
        if($("#id").val()){
            removefontSource($("#id").val())
        }else{

        }
    }
    //保存
    function submitHandler() {
        if ($.validate.form()) {
            var params = $("#fieldForm").serializeObject()
            if ($("#id").val()) {
                params = Object.assign($("#fieldForm").serializeObject(), {id: $("#id").val()})
            }
            if($(".is-page-store").length==1){
                params = Object.assign(params, {isPageStore: $(".isPageStore").prop("checked")?'Y':'N'})
            }
            var config = {
                url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/saveFieldSource",
                type: "POST",
                dataType: "json",
                async:false,
                contentType: "application/json",
                data: JSON.stringify(params),
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    $.modal.disable();
                },
                success: function (result) {
                    $.modal.closeLoading();
                    if (result.code == web_status.SUCCESS) {
                        $.modal.close();
                        return false;
                    }
                    if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg);
                        return false;
                    }
                    $.modal.alertError(result.msg);
                }
            };
            $.ajax(config);
        }
    }
    // 获取入参来源设置详情
    function detail(){
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/detail",
            type: "get",
            dataType: "json",
            data: {
                id:$("#pageId").val(),
                pageImgId:$("input[name='pageImgId']").val(),
                interfaceFieldId:$("#interfaceFieldId").val(),
                type:$("input[name='objectType']").val()
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var formDatailTplHtml = $("#formDatailTpl").html();
                    laytpl(formDatailTplHtml).render(result.data, function (html) {
                        $("#fieldForm").append(html);
                    });
                    $("#id").val(result.data.id||'');
                    if(!result.data.id){
                        return false;
                    }
                    if(result.data.sourceType==1){
                        window[selectOneActions[1]](result.data);
                        queryInterfaceAliasNames(result.data);
                        queryBffInterfaceField(result.data.sourceInterfaceId,result.data);
                        result.data.sourceDictType && querySourceInterfaceContainCategory(result.data.sourcePageImgId,result.data);
                        // result.data.sourceDictValue && queryDict(result.data);
                    }else if(result.data.sourceType==2){
                        conventinalOption(result.data.contractValueType);
                        window[conventinalData.filter(e=>e.index==result.data.contractValueType)[0].action](result.data);
                        if(result.data.contractValueType==0){
                            queryDictType(result.data);
                            //queryDict(result.data)
                        }
                        if(result.data.contractValueType==6){
                            queryDict(result.data);
                        }
                    }else{
                        queryDict(result.data)
                        interfaceList(result.data,result.data.storageType);
                        queryBffInterfaceField(result.data.sourceInterfaceId,result.data);
                    }
                }
            }
        };
        $.ajax(config);
    }
    // 获取入参列表
    function interfaceList(selectObj,storageType){
        var params = { pageId:$("#pageId").val()}
            storageType && Object.assign(params,{storageType:storageType})
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryInterfaceByPageId",
            type: "get",
            dataType: "json",
            async:false,
            data: params,
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var paramsTplHtml = $("#interfaceOptionTpl").html();
                    if(selectObj){
                        result.sourceInterfaceId = selectObj.sourceInterfaceId;
                        result.sourceInterfaceUrl = selectObj.sourceInterfaceUrl;
                        result.sourceInterfaceName = selectObj.sourceInterfaceName;
                    }
                    if(storageType){
                        result.type = "storage-three";
                        laytpl(paramsTplHtml).render(result, function (html) {
                            $("#select-three").html('')
                            $("#select-three").html(html);
                        });
                    }else{
                        result.type = "two";
                        laytpl(paramsTplHtml).render(result, function (html) {
                            $("#select-two").html('')
                            $("#select-two").html(html);
                        });
                        $("#select-four").html(`<div class="col-sm-12 is-page-store" style="text-align: right;padding: 0"><input type="checkbox" class="isPageStore"  ${selectObj && selectObj.isPageStore=='N'?'':'checked'}>是否页面store</div>`);
                    }
                    $('#interfaceList').select2({
                        placeholder: "选择接口",
                        allowClear: false
                    }).on('select2:select', function (e) {
                        $("input[name='sourceInterfaceName']").val(e.params.data.element.dataset.name)
                        $("input[name='sourceInterfaceUrl']").val(e.params.data.element.dataset.url)
                    });
                }
            }
        };
        $.ajax(config);
    }
    // 约定值
    function conventinalOption(contractValueType) {
        //var data = ["枚举父类","字段枚举类型","表名","主键字段名","修订字段名","对象属性值","主键标识"]
        var paramsTplHtml = $("#conventinalOptionTpl").html();
        laytpl(paramsTplHtml).render({contractValueType:contractValueType,data:conventinalData}, function (html) {
            $("#select-two").html('')
            $("#select-two").html(html);
        });
    }
    // 获取接口参数
    function queryBffInterfaceField(interfaceId,selectObj,isInParam) {
        isInParam = isInParam || 2;
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryBffInterfaceField",
            type: "get",
            dataType: "json",
            data: {
                interfaceId:interfaceId,
                isInParam:isInParam
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var paramsTplHtml = $("#fieldTpl").html();
                    if(selectObj && Object.keys(selectObj).length>0){
                        result.sourceObjectId = selectObj.sourceObjectId
                        result.sourceObjectName = selectObj.sourceObjectName
                        result.sourceObjectEnName = selectObj.sourceObjectEnName
                        result.sourceObjectFieldId = selectObj.sourceObjectFieldId
                        result.sourceObjectFieldName = selectObj.sourceObjectFieldName
                        result.sourceObjectFieldEnName = selectObj.sourceObjectFieldEnName
                    }
                    if($(".select-storage-three").length==1){
                        laytpl(paramsTplHtml).render(result, function (html) {
                            $("#select-four").html('')
                            $("#select-four").html(html);
                        });
                    }else{
                        laytpl(paramsTplHtml).render(result, function (html) {
                            $("#select-three").html('')
                            $("#select-three").html(html);
                        });
                    }
                    if(selectObj && selectObj.sourceObjectId){
                        var selectDom = "a[data-objid='"+selectObj.sourceObjectId+"']";
                        if(selectObj.sourceObjectFieldId && selectObj.sourceObjectFieldName){
                            selectDom = "a[data-objfieldid='"+selectObj.sourceObjectFieldId+"']";
                        }
                        $("#select-title").text($(".dropdown li "+selectDom).html());
                    }
                }
            }
        };
        $.ajax(config);
    }
    // 获取编码
    function queryFieldDict(selectObj) {
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryFieldDict",
            type: "get",
            dataType: "json",
            data: {
                interfaceFieldId:$("#interfaceFieldId").val(),
                type:$("input[name='objectType']").val()
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var paramsTplHtml = $("#fieldDictOptionTpl").html();
                    if(selectObj){
                        result.dictValue = selectObj.dictValue||''
                    }
                    laytpl(paramsTplHtml).render(result, function (html) {
                        $("#select-three").html('')
                        $("#select-three").html(html);
                    });
                }
            }
        };
        $.ajax(config);
    }
    // 获取表
    function tableList(selectObj) {
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/tableList",
            type: "get",
            dataType: "json",
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var paramsTplHtml = $("#tableListTpl").html();
                    if(selectObj){
                        result.tableOrFieldId = selectObj.tableOrFieldId;
                        result.tableOrFieldName = selectObj.tableOrFieldName
                    }
                    laytpl(paramsTplHtml).render(result, function (html) {
                        $("#select-three").html('')
                        $("#select-three").html(html);
                    });
                    $('#tableList').select2({
                        placeholder: "选择接口",
                        allowClear: false
                    }).on('select2:select', function (e) {
                        $("input[name='tableOrFieldName']").val(e.params.data.title)
                    });
                }
            }
        };
        $.ajax(config);
    }
    // 获取主键
    function pkFieldList(selectObj) {
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/pkFieldList",
            type: "get",
            dataType: "json",
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    if(selectObj){
                        result.tableOrFieldId = selectObj.tableOrFieldId;
                        result.tableOrFieldName = selectObj.tableOrFieldName
                    }
                    var paramsTplHtml = $("#pkFieldListTpl").html();
                    laytpl(paramsTplHtml).render(result, function (html) {
                        $("#select-three").html('')
                        $("#select-three").html(html);
                    });
                    $('#pkFieldList').select2({
                        placeholder: "选择主键名",
                        allowClear: false
                    }).on('select2:select', function (e) {
                        $("input[name='tableOrFieldName']").val(e.params.data.title)
                    });
                }
            }
        };
        $.ajax(config);
    }
    function ObjAttrCode(selectObj) {
        queryBffInterfaceField($("input[name='interfaceId']").val(),selectObj,1)
    }
    function ObjAttr(selectObj) {
        var ObjAttrTplHtml = $("#ObjAttrTpl").html();
        var result = {}
        if(selectObj){
            result.contractValue = selectObj.contractValue||''
        }
        laytpl(ObjAttrTplHtml).render(result, function (html) {
            $("#select-three").html('')
            $("#select-three").html(html);
        });
    }
    function queryDict(selectObj) {
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryDict",
            type: "post",
            dataType: "json",
            data: {
                dictType:$("select[name='dictType']").val()||$("select[name='sourceDictType']").val()||"storageType",
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var queryDictOptionTplHtml = $("#queryDictOptionTpl").html();
                    if($(".select-one").val()==2 && ['0','6'].indexOf($(".select-two").val()) >= 0){
                        result.name = "dictValue"
                        if(selectObj){
                            result.storageType = selectObj.dictValue
                        }
                        laytpl(queryDictOptionTplHtml).render(result, function (html) {
                            $("#select-four").html('')
                            $("#select-four").html(html);
                        });
                    }else if($("#sourceDictType").length==1){
                        if(!result.data || result.data.length==0){
                            return false;
                        }
                        queryDictOptionTplHtml = $("#sourceDictValueTpl").html();
                        if(selectObj){
                            result.sourceDictValue = selectObj.sourceDictValue
                        }
                        laytpl(queryDictOptionTplHtml).render(result, function (html) {
                            $("#sourceDictValue").remove();
                            $("#sourceDictType").after(html);
                        });
                    }else {
                        if(selectObj){
                            result.storageType = selectObj.storageType
                        }
                        laytpl(queryDictOptionTplHtml).render(result, function (html) {
                            $("#select-two").html('')
                            $("#select-two").html(html);
                        });
                    }

                }
            }
        };
        $.ajax(config);
    }
    function removefontSource(ids){
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/remove",
            type: "post",
            dataType: "json",
            async:false,
            data:{
                ids:ids
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    $(".form-group").remove()
                    // detail();
                    $.modal.close();
                }
            }
        };
        $.ajax(config);
    }
    function queryDictType(selectObj){
        var category;
        // 主键标志
        if($(".select-one").val()==2 && $(".select-two").val() == 6){
            category = 4;
        }else if(selectObj){
            category = selectObj.dictCategory;
        }else{
            category = $("select[name='dictCategory']").val()
        }
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/getDictListByCategory",
            type: "get",
            dataType: "json",
            async:false,
            data:{
                category:category
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var queryDictTypeOptionTplHtml = $("#queryDictTypeOptionTpl").html();
                    if(selectObj){
                        result.dictType = selectObj.dictType
                    }
                    console.log(queryDictTypeOptionTplHtml);
                    laytpl(queryDictTypeOptionTplHtml).render(result, function (html) {
                        $("#select-three").html('')
                        $("#select-three").html(html);
                    });
                    $('#dictTypes').select2({
                        placeholder: "选择字典类型",
                        allowClear: false
                    })
                }
            }
        };
        $.ajax(config);
    }
    /**
     * 获取字典分类
     * @param selectObj
     */
    function  getDictCategory(selectObj) {
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/getDictCategory",
            type: "get",
            dataType: "json",
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    $("#conventinal").removeClass("col-sm-12").addClass("col-sm-6");
                    $("#select-two").removeClass("col-sm-3").addClass("col-sm-5");
                    var dictCategoryTplHtml = $("#dictCategoryTpl").html();
                    if(selectObj){
                        result.dictCategory = selectObj.dictCategory
                    }
                    laytpl(dictCategoryTplHtml).render(result, function (html) {
                        $("#dictCategory").remove();
                        $("#select-two").append(html);
                    });
                }
            }
        };
        $.ajax(config);
    }
    /**
     * 根据接口id和页面id，返回在页面所有关于这个接口的别名集合
     */
    function queryInterfaceAliasNames(selectObj){
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryInterfaceAliasNames",
            type: "get",
            dataType: "json",
            async:false,
            data: {
                interfaceId:$("#interfaceList").val(),
                pageId:$("#pageId").val()
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS && result.data.length>0) {
                    $("#interfaceList").parent().removeClass("col-sm-12").addClass("col-sm-6");
                    $("#select-two").removeClass("col-sm-3").addClass("col-sm-5");
                    var queryInterfaceAliasNamesTplHtml = $("#queryInterfaceAliasNamesTpl").html();
                    if(selectObj){
                        result.sourcePageImgId = selectObj.sourcePageImgId
                    }
                    laytpl(queryInterfaceAliasNamesTplHtml).render(result, function (html) {
                        $("#interfaceAliasNames").remove();
                        $("#select-three").html('');
                        $("#select-two").append(html);
                    });
                }else{
                    //获取来源接口的入参中包含类型
                    querySourceInterfaceContainCategory();
                    $("#interfaceList").parent().addClass("col-sm-12").removeClass("col-sm-6");
                    $("#select-two").addClass("col-sm-3").removeClass("col-sm-5");
                    $("#interfaceAliasNames").remove();
                }
            }
        };
        $.ajax(config);
    }

    /**
     * 来源接口的入参中包含类型
     * @param sourcePageImgId
     * @param selectObj
     */
    function querySourceInterfaceContainCategory(sourcePageImgId,selectObj){
        sourcePageImgId = sourcePageImgId ||'';
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/querySourceInterfaceContainCategory",
            type: "get",
            dataType: "json",
            data:{
                interfaceId:$("#interfaceList").val(),
                pageId:$("#pageId").val(),
                sourcePageImgId:sourcePageImgId
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    if(result.data.length==0){
                        return false;
                    }
                    var tplHtml = $("#sourceDictTypeTpl").html();
                    result.sourceDictType = selectObj?selectObj.sourceDictType:result.data[0].dictType;
                    laytpl(tplHtml).render(result, function (html){
                        $("#sourceDictType").remove();
                        $("#select-four").after(html);
                    });
                    queryDict(selectObj);
                }
            }
        };
        $.ajax(config);
    }
</script>
</body>
</html>
